<?php

/**
 *	media slider
 *	frontend module that generates slider of given object
 *	content and slider options are defined by the vars in the database json string
 *	layout is easily manipulable by the style.css or local modules.css
 *
 * 	the class is documented in the file itself. if you find any bugs help us out and report them. 
 * 	if you report a bug, make sure you give us enough information (include your code).
 *
 *	see for an example at the bottom at this file
 *
 *	@package		engine one - frontend modules
 *
 *	@copyright		oneDot Only <we@onedotonly.be>
 *	@author			pieter van peteghem <pieter@onedotonly.be>
 *  @license		BSD licens
 *
 *	@date			7th january 2011
 */

class MediaSlider extends MOD {

	/*
	 *	constructor
	 *	runs before the html cycle is started
	 *  @param		int		id
	 *	@param		object	Object
	 */
	public function construct() {
		global $Object, $Page;
	
		if( !$id = (int) $_GET['id'] ) 
			return false;
		
		if( !$Object )
			$Page->set_cache( "title", $Album->get( 'name' ));
		
		else
			$Object->_addvars( $this->vars );

	}

	
	
	/**
	 * 	output
	 *	@param		object 	database json string
	 * 	@output		string	html code
	 */
	public function output() {
		global $Album, $Object;
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();
		
		if( $result ) {
			$this->addclass( $this->action );
			return $this->parse( $result ) . $this->render_js();
		}
	}
	
	
	public function filter_simple() {
		global $DB, $Object;
		
		$elements	= $DB->filter_objects( $this->vars->types, $this->vars->condt );

		$this->js = "
			mod = new MediaSlider( " . json_encode( $this->vars->nav ) . ");
			mod.init();
		";
		return $this->render_list( $elements, $this->vars->id, $this->vars->width, $this->vars->height, $this->vars->nav->types, (( $this->vars->href ) ? $this->vars->href : false ), (( $this->vars->date ) ? $this->vars->date : 'd. m' ), (( $this->vars->img ) ? $this->vars->img : false ));
	}
	
	
	public function filter_json() {
		
		// fetch content
		$content	= json_decode( @file_get_contents( $this->vars->url ));
		
		// fetch content by key
		$objects	= (array) $content->{ $this->vars->key };
		
		// parse list
		$i = 0;
		foreach( $objects as $object ) {
			$ids[ $i ][ 'date' ] = $object->date;		
			$ids[ $i ][ 'name' ] = $object->name;		
			$ids[ $i ][ 'url' ] = $object->url;		
			$ids[ $i ][ 'rank' ] = $object->rank;		
			$ids[ $i ][ 'img' ] = $object->img;		
			$i++;	
		}
		
		//$elements = $DB->filter_objects( $this->vars->types, 'ID in( ' . implode( ', ', $ids ) . ' )');
		$this->js = "
			mod = new MediaSlider( " . json_encode( $this->vars->nav ) . ");
			mod.init();
		";
		return $this->render_static_list( $ids, $this->vars->id, $this->vars->width, $this->vars->height, $this->vars->nav->types, (( $this->vars->href ) ? $this->vars->href : false ), (( $this->vars->date ) ? $this->vars->date : 'd. m' ), (( $this->vars->img ) ? $this->vars->img : false ), $this->vars->title, $this->vars->logo );
	}
	


	/**
	 *	render slider
	 * 	@param		array	nodes
	 * 	@param		int		width
	 *	@param		int		height
	 *	@param		object	navigation types
	 *	@param		boolean uri
	 *	@param		date	type of date
	 *	@return		html string
	 */
	private function render_static_list( $nodes, $id, $width, $height, $nav, $uri = true, $date = 'd. m', $img_dim = false, $title = false, $logo = false ) {
		global $DB, $Object, $Cal;
	
		$res .= '<div id="' . $id . '" class="slider-container" style="width:' . $width . 'px; height:' . $height . 'px;">';
		
		// navigation
		if( $nav->arrows ) {
			$res .= '<span class="left"></span>';
			$res .= '<span class="right"></span>';
		}
		if( $nav->numbers || $nav->dots ) {
			$res .= '<span class="numbers' . (( $nav->dots ) ? ' dots' : '' ) . '">';
			
			$i = 0; $t = 0;
			foreach( $nodes as $node ) {
				$res .= '<a href="#" onclick="return false;" rel="' . ( $t + 1 ) . '" class="number' . (( $i == 0 ) ? ' active' : '' ) . '">';
				if( $nav->numbers )
					$res .= $i + 1;
					
				$res .= '</a>'; 
				$i++; $t++;
			}
			$res .= '</span>';
		}
		
		
		// slider content
		$res .= '<ul class="slider-ul" style="padding: 0;width:' . ( count( $nodes ) * $width ) . 'px; height:' . $height . 'px;">';
		
		foreach( $nodes as $node ) {
		
			$back  = '';
			if( $img = $node[ 'img' ])
				$back = " background-image: url(" . substr( $img, 0, -7 ) . (( $img_dim ) ? $img_dim : $width . "." . $height ) . ")";
		
			if( $back ) {
				$item  = '';
				$item .= '<li class="slider-item" style="width:' . $width . 'px; height:' . $height . 'px;' . $back . '">';
				if( $logo ) {
					$item .= '<span class="slider-item-logo"><img src="' . $logo . '" /></span>';
				}
				$item .= '<span class="slider-item-inner">';
				if( $title ) {
					$item .= '<span class="over slider-item-title">' . $title . '</span>';
				}
				foreach( $this->vars->content as $content ) {
					if( $node[ $content ] && strlen( $node[ $content ]) > 3 ) {
						$item .= '<span class="over slider-item-' . $content . '">' . (( $content == 'date' ) ? date( $date, strtotime( $node[ $content ])) : $node[ $content ]) . '</span>';
					}	
				}			
				$item .= '</span></li>';
			
				if( $uri ) {
					$item = '<a target="_blank" href="' . $node[ 'url' ] . '">' . $item . '</a>';
				}

				$res .= $item;
			}
		}
		
		$res .= '</ul>';
		$res .= '</div>';
		
		return $res;
	}
	
	


	/**
	 *	render slider
	 * 	@param		array	nodes
	 * 	@param		int		width
	 *	@param		int		height
	 *	@param		object	navigation types
	 *	@param		boolean uri
	 *	@param		date	type of date
	 *	@return		html string
	 */
	private function render_list( $nodes, $id, $width, $height, $nav, $uri = true, $date = 'd. m', $img_dim = false ) {
		global $DB, $Object, $Cal;
	
		$res .= '<div id="' . $id . '" class="slider-container" style="width:' . $width . 'px; height:' . $height . 'px;">';
		
		// navigation
		if( $nav->arrows ) {
			$res .= '<span class="left"></span>';
			$res .= '<span class="right"></span>';
		}
		if( $nav->numbers || $nav->dots ) {
			$res .= '<span class="numbers' . (( $nav->dots ) ? ' dots' : '' ) . '">';
			
			$i = 0; $t = 0;
			foreach( $nodes as $node ) {
				$res .= '<a href="#" onclick="return false;" rel="' . ( $t + 1 ) . '" class="number' . (( $i == 0 ) ? ' active' : '' ) . '">';
				if( $nav->numbers )
					$res .= $i + 1;
					
				$res .= '</a>'; 
				$i++; $t++;
			}
			$res .= '</span>';
		}
		
		
		// slider content
		$res .= '<ul class="slider-ul" style="width:' . ( count( $nodes ) * $width ) . 'px; height:' . $height . 'px;">';
		
		foreach( $nodes as $node ) {
		
			$back  = '';
			if( $img = $node->media->get( 'media.img' )->ID);
				$back = " background-image: url(/public.img?id=" . $img . "&sze=" . (( $img_dim ) ? $img_dim : $width . "." . $height ) . ")";
		
			if( $node->get( 'otype' ) && $node->get( 'objid' )) {
				$back = '';
				$node = new CALNODE( $DB->type_r( $node->get( 'type' )), (int) $node->get( 'ID' ));
				if( $node->obj->hasMedia())
					$back = " background-image: url(/public.img?id=" . $node->obj->media->get( 'media.img' )->ID . "&sze=" . (( $img_dim ) ? $img_dim : $width . "." . $height ) . ")";
			}	
			
			if( $back ) {
				$item  = '';
				$item .= '<li class="slider-item" style="width:' . $width . 'px; height:' . $height . 'px;' . $back . '">';
				$item .= '<span class="slider-item-inner">';
				foreach( $this->vars->content as $content ) {
					if( $node->get( $content ) && strlen( $node->get( $content )) > 3 ) {
						$item .= '<span class="over slider-item-' . $content . '">' . (( $content == 'date' ) ? date( $date, strtotime( $node->get( $content ))) : $node->get( $content )) . '</span>';
					} else if( $node->obj ) {
						$item .= '<span class="over slider-item-' . $content . '">' . $node->obj->values[ $content ] . '</span>';
					}	
				}			
				$item .= '</span></li>';
			
				if( $uri ) {
					$item = '<a href="' . $node->uri() . '">' . $item . '</a>';
				}

			
				$res .= $item;
			}
		}
		
		$res .= '</ul>';
		$res .= '</div>';
		
		return $res;
	}
	

}


/*
{
    "mod": "media.slider",
    "action": "simple",
    "vars": {
        "types": "cal.single",																						// string or array
        "condt": "active = 1 AND otype = 1 AND date > NOW() AND objid != 0 order by date ASC limit 5",				// mysql condt
        "width": "210",																								// int width
        "height": "260",																							// int height
        "id": "slider",																								// id for the slider 
        "content": [			
            "name",																									// define what should be displayed on top of the img
            "date"
        ],
        "href": "true",																								// with link or not?
        "date": "d. m",																								// transform date
        "nav": {	
            "effect": "slider",																						// slider or fader - not effect means no effect
            "types": {
                "numbers": false,																					// navigation possibilities - numbers / arrows
                "arrows": false
            },
            "delay": "5",																							// when played delay between effects
            "duration": "0.4",																						// time of effect
            "play": "auto"																							// player on?
        }
    }
}
*/